using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Runtime.Serialization;

namespace WcfService1.DTO
{
    [DataContract]
    public class SalesInvoiceDTO : INotifyPropertyChanged
    {
        private Decimal _AfterDiscount;
        private CostCenterDTO _CostCenter;
        private CurrencyDTO _Currency;
        private CustomerDTO _Customer;
        private DeliveryOrderDTO _DeliveryOrder;
        private Decimal _Discount;
        private String _DiscountType;
        private DateTime? _InvoiceDate;
        private bool _IsPpn;
        private String _Keterangan;
        private int _No;
        private Decimal _Ppn;
        private Decimal _Rate;
        private String _SINo;
        private ObservableCollection<SalesInvoiceDetailDTO> _SalesInvoiceDetails;
        private EmployeeDTO _Salesman;
        private Decimal _SubTotal;
        private String _Term;
        private Decimal _Total;
        private String _created_by;
        private DateTime? _created_date;
        private String _modified_by;
        private DateTime? _modified_date;
        private string _NamaDepan;
        private string _CustomerName;
        private string _SPKNo;
        private string _PSNo;
        private string _DONo;
        private string _SONo;
        private Status _Status;
        private string _Departemen;
        private string _CurrencyCode;
        private decimal _TotalRp;

        public SalesInvoiceDTO()
        {
            InvoiceDate = DateTime.Now.Date;
        }


        [DataMember]
        public String SINo
        {
            get { return _SINo; }
            set
            {
                _SINo = value;
                RaisePropertyChanged("SINo");
            }
        }


        [DataMember]
        public DateTime? InvoiceDate
        {
            get { return _InvoiceDate; }
            set
            {
                _InvoiceDate = value;
                RaisePropertyChanged("InvoiceDate");
            }
        }


        [DataMember]
        public DeliveryOrderDTO DeliveryOrder
        {
            get { return _DeliveryOrder; }
            set
            {
                if (ReferenceEquals(value,_DeliveryOrder) || value == null)return;
               
                    _DeliveryOrder = value;
                    RaisePropertyChanged("DeliveryOrder");
            }
        }


        [DataMember]
        public CustomerDTO Customer
        {
            get { return _Customer; }
            set
            {
                _Customer = value;
                RaisePropertyChanged("Customer");
            }
        }


        [DataMember]
        public CurrencyDTO Currency
        {
            get { return _Currency; }
            set
            {
                _Currency = value;
                RaisePropertyChanged("Currency");
            }
        }


        [DataMember]
        public EmployeeDTO Salesman
        {
            get { return _Salesman; }
            set
            {
                _Salesman = value;
                RaisePropertyChanged("Salesman");
            }
        }


        [DataMember]
        public Decimal Rate
        {
            get { return _Rate; }
            set
            {
                _Rate = value;
                RaisePropertyChanged("Rate");
            }
        }


        [DataMember]
        public String Term
        {
            get { return _Term; }
            set
            {
                _Term = value;
                RaisePropertyChanged("Term");
            }
        }


        [DataMember]
        public Decimal Ppn
        {
            get { return _Ppn; }
            set
            {
                _Ppn = value;
                RaisePropertyChanged("Ppn");
            }
        }


        [DataMember]
        public String Keterangan
        {
            get { return _Keterangan; }
            set
            {
                _Keterangan = value;
                RaisePropertyChanged("Keterangan");
            }
        }

        [DataMember]
        public String PSNo
        {
            get { return _PSNo; }
            set
            {
                _PSNo = value;
                RaisePropertyChanged("PSNo");
            }
        }

        [DataMember]
        public String SONo
        {
            get { return _SONo; }
            set
            {
                _SONo = value;
                RaisePropertyChanged("SONo");
            }
        }

        [DataMember]
        public String DONo
        {
            get { return _DONo; }
            set
            {
                _DONo = value;
                RaisePropertyChanged("DONo");
            }
        }

        [DataMember]
        public String SPKNo
        {
            get { return _SPKNo; }
            set
            {
                _SPKNo = value;
                RaisePropertyChanged("SPKNo");
            }
        }


        [DataMember]
        public String created_by
        {
            get { return _created_by; }
            set
            {
                _created_by = value;
                RaisePropertyChanged("created_by");
            }
        }


        [DataMember]
        public DateTime? created_date
        {
            get { return _created_date; }
            set
            {
                _created_date = value;
                RaisePropertyChanged("created_date");
            }
        }

        [DataMember]
        public String modified_by
        {
            get { return _modified_by; }
            set
            {
                _modified_by = value;
                RaisePropertyChanged("modified_by");
            }
        }


        [DataMember]
        public DateTime? modified_date
        {
            get { return _modified_date; }
            set
            {
                _modified_date = value;
                RaisePropertyChanged("modified_date");
            }
        }

        [DataMember]
        public ObservableCollection<SalesInvoiceDetailDTO> SalesInvoiceDetails
        {
            get { return _SalesInvoiceDetails; }
            set
            {
                if (value == SalesInvoiceDetails) return;
                _SalesInvoiceDetails = value;
                RaisePropertyChanged("SalesInvoiceDetails");
                RefreshNumber();
            }
        }

        [DataMember]
        public Decimal Discount
        {
            get { return _Discount; }
            set
            {
                _Discount = value;
                RaisePropertyChanged("Discount");
            }
        }


        [DataMember]
        public String DiscountType
        {
            get { return _DiscountType; }
            set
            {
                _DiscountType = value;
                RaisePropertyChanged("DiscountType");
            }
        }

        public int No
        {
            get { return _No; }
            set
            {
                _No = value;
                RaisePropertyChanged("No");
            }
        }

        [DataMember]
        public Decimal Total
        {
            get { return _Total; }
            set
            {
                _Total = value;
                RaisePropertyChanged("Total");
            }
        }

        [DataMember]
        public Decimal TotalRp
        {
            get { return _TotalRp; }
            set
            {
                _TotalRp = value;
                RaisePropertyChanged("TotalRp");
            }
        }

        public bool IsPpn
        {
            get { return _IsPpn; }
            set
            {
                _IsPpn = value;
                RaisePropertyChanged("IsPpn");
            }
        }

        public Decimal SubTotal
        {
            get { return _SubTotal; }
            set
            {
                _SubTotal = value;
                RaisePropertyChanged("SubTotal");
            }
        }

        public Decimal AfterDiscount
        {
            get { return _AfterDiscount; }
            set
            {
                _AfterDiscount = value;
                RaisePropertyChanged("AfterDiscount");
            }
        }

        [DataMember]
        public CostCenterDTO CostCenter
        {
            get { return _CostCenter; }
            set
            {
                _CostCenter = value;
                RaisePropertyChanged("CostCenter");
            }
        }

        [DataMember]
        public String CustomerName
        {
            get { return _CustomerName; }
            set
            {
                _CustomerName = value;
                RaisePropertyChanged("CustomerName");
            }
        }

        [DataMember]
        public String NamaDepan
        {
            get { return _NamaDepan; }
            set
            {
                _NamaDepan = value;
                RaisePropertyChanged("NamaDepan");
            }
        }

         [DataMember]
        public Status Status
        {
            get { return _Status; }

            set
            {
                _Status = value;
                RaisePropertyChanged("Status");
            }
        }

         [DataMember]
         public String Departemen
         {
             get { return _Departemen; }
             set
             {
                 _Departemen = value;
                 RaisePropertyChanged("Departemen");
             }
         }

         [DataMember]
         public String CurrencyCode
         {
             get { return _CurrencyCode; }
             set
             {
                 _CurrencyCode  = value;
                 RaisePropertyChanged("CurrencyCode");
             }
         }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private void RefreshNumber()
        {
            SalesInvoiceDetails.CollectionChanged += SalesInvoiceDetails_CollectionChanged;
            int number = 0;
            foreach (SalesInvoiceDetailDTO DetailDto in SalesInvoiceDetails)
            {
                number += 1;
                DetailDto.No = number;
            }
        }

        private void SalesInvoiceDetails_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            int number = 0;
            foreach (SalesInvoiceDetailDTO DetailDto in SalesInvoiceDetails)
            {
                number += 1;
                DetailDto.No = number;
            }
        }

        public void RaisePropertyChanged(string propertyname)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyname));
        }

        public override string ToString()
        {
            return this.SINo;
        }
    }
}